<template>
	<view>
		<!-- <view v-show="isShow">
			<progress :percent="percent" stroke-width="10"></progress>
		</view> -->
		<view>
			<button type="primary" :loading="loading" :disabled="disabled" @click="upload" :class="{ active : active }"
				:style="{'height':height,'line-height':lineheight,'border-radius':borderRadius}">
				<image class="headimg-img" :src="src" :style="{'height':height,'width':width}"></image>
			</button>
		</view>
	</view>

</template>
<script>
	import {
		mapState
	} from 'vuex';
	import {
		nativePopup
	} from "@/common/native_popup.js";
	// import permision from "@/common/permission.js";
	var _self;
	export default {
		data() {
			return {
				isShow: false,
				percent: 0,
				loading: false,
				disabled: false,
				active: false,
			}
		},
		props: ['src', 'width', 'height', 'lineheight', 'borderRadius'],
		created() {

		},
		computed: {
			...mapState(['hasLogin']),
		},
		methods: {
			async upload() {
				if (!this.hasLogin) {
					this.$api.msg('请先登录！', 'none');
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login',
						})
					}, 1000)
					return false;
				}
				// this.isShow = true;
				// uni.setStorageSync('isCamera', false)
				if (uni.getSystemInfoSync().platform === "android") {
					let isCameraApply = uni.getStorageSync('isCameraApply');
					let isAlbumApply = uni.getStorageSync('isAlbumApply');
					console.log(isCameraApply)
					console.log(isAlbumApply)
					if (isCameraApply !== true) {
						nativePopup.show({
							title: '权限申请说明',
							content: '为了修改个人头像和发布信息图片视频等, 我们需要申请您设备的相机和存储权限'
						})
					}
					if (isAlbumApply !== true) {
						nativePopup.show({
							title: '权限申请说明',
							content: '为了修改个人头像和发布信息图片视频等, 我们需要申请您设备的相机和存储权限'
						})
					}
				}
				this.active = true;
				let token = uni.getStorageSync('token');

				_self = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					// sourceType: ['album'], //从相册选择
					success: function(res) {
						if (uni.getSystemInfoSync().platform === "android") {
							let firstPath = res.tempFilePaths[0]
							console.log(firstPath)
							// console.log(firstPath.includes('camera'))
							if (firstPath.includes('camera')) {
								// console.log('用户是通过拍照获取的图片');
								uni.setStorageSync('isCameraApply', true);
							} else {
								// console.log('用户是从相册中选择的图片');
								uni.setStorageSync('isAlbumApply', true);
							}
						}
						const tempFilePaths = res.tempFilePaths;
						const uploadTask = uni.uploadFile({
							url: 'https://zhgxapp.hnliandesy.com/api/upload',
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {

							},
							success: function(uploadFileRes) {
								if (uploadFileRes.data) {
									uni.$emit('changeHeadimg', uploadFileRes.data);
								}
							}
						});

						uploadTask.onProgressUpdate(function(res) {
							_self.percent = res.progress;
							// this.$api.msg('上传进度为 '.this.percent)
						});
					},
					error: function(e) {
						console.log(e);
					},
					complete() {
						nativePopup.close()
					}
				});
			}
		},
		onLoad: function() {

		}
	}
</script>

<style scoped>
	uni-button[type=primary] {
		color: #fff;
		background-color: #fff;
		height: 194rpx;
	}

	.headimg-img {
		/* margin-left: -30rpx; */
		border-radius: 50%;
		// #ifdef MP-WEIXIN
		margin-left: 0;
		// #endif
	}

	button {
		// #ifdef MP-WEIXIN
		padding-left: 0;
		padding-right: 0;
		// #endif
		background-color: #FFFFFF;
	}
</style>